﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Example6_Particles.View
{
    /**
     * Statefull particle system
     */
    class ExplosionSystem
    {
        
        private const int NUM_PARTICLES = 100;
        
        //An explosion takes this amount of time
        float m_maxTime = 0.5f;

        //the particle state
        private ExplosionParticle[] m_particles = new ExplosionParticle[NUM_PARTICLES];

        public ExplosionSystem()
        {
            MakeParticleSystem();
        }

        //Initiate the particle state
        private void MakeParticleSystem()
        {
            //Create particle system
            Random rand = new Random();
            for (int i = 0; i < NUM_PARTICLES; i++)
            {
                //Create a circular random speed vector with max radius 2
                float maxSpeed = 2.5f;
                //get a squared random vector from -0.5 to 0.5
                Vector2 randomDirection = new Vector2((float)rand.NextDouble() - 0.5f, (float)rand.NextDouble() - 0.5f);
                //normalize to get it spherical vector with length 1.0
                randomDirection.Normalize();
                //add random length between 0 to maxSpeed
                randomDirection = randomDirection * (float)rand.NextDouble() * maxSpeed;


                m_particles[i] = new ExplosionParticle(randomDirection);
            }
            
        }


        public void DrawParticleSystem(float a_totalTime, Vector2 a_position, float a_viewScale, SpriteBatch a_batch, Texture2D a_texture)
        {
            

            for (int i = 0; i < NUM_PARTICLES; i++)
            {
                //Camera translation
                Vector2 viewPos = m_particles[i].GetPosition(a_totalTime) * a_viewScale + a_position;
               
                //interpolate from 1 to 0 depending on time
                float percentLeft = 1.0f - a_totalTime / m_maxTime;

                //different sizes depending on index (i % 80) and a minimum size of 10 and particles are growing with respect to time
                float size = (i % 80 + 10) * (a_totalTime / m_maxTime);

                DrawParticleAt(viewPos, size, percentLeft, a_batch, a_texture);
                

            }


            
        }

        private void DrawParticleAt(Vector2 a_viewPos, float a_size, float a_alpha, SpriteBatch a_batch, Texture2D a_texture)
        {
            //the entire texture as source
            Rectangle srcRect = new Rectangle(0,0, a_texture.Width, a_texture.Height);

            //color fades to 0
            Color color = new Color(a_alpha, a_alpha, a_alpha, a_alpha);

            //rotation depending on color
            float rotation = a_alpha * 3;

            //rotate around middle of texture
            Vector2 origin = new Vector2(a_texture.Width / 2, a_texture.Height / 2);

            //size is relative to texture size in draw
            float size = a_size / a_texture.Width; 

            //and draw it
            a_batch.Draw(a_texture, a_viewPos, srcRect, color, rotation, origin, size, SpriteEffects.None, 0);
            
        }
    }
}
